package com.rabbit.producer.broker;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * 线程池
 *
 * @author: 顾志杰
 */
@Slf4j
public class AsyncBaseQueue {

    /**
     * 线程池大小
     */
    private static final int THREAD_SIZE = Runtime.getRuntime().availableProcessors();

    /**
     * 队列大小
     */
    private static final int QUEUE_SIZE = 10000;

    /**
     * 异步发送
     */
    private static ExecutorService senderAsync = new ThreadPoolExecutor(
            THREAD_SIZE,
            THREAD_SIZE,
            60L,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(QUEUE_SIZE),
            /*成功*/
            r -> {
                Thread thread = new Thread(r);
                thread.setName("rabbitmq_client_async_sender");
                return thread;
            },
            /*失败*/
            (r, executor) -> log.info("async sender is error rejected, runnable:{}, executor:{}",r,executor)
    );

    public static void submit(Runnable runnable){
        senderAsync.submit(runnable);
    }

}
